var api = require('../../../utils/api.js')
var $ = require('../../../utils/util.js')
var regeneratorRuntime = require('../../../utils/runtime.js')
var app = getApp()

Page({
  data: {
    username:'',
    password:'',
    imgUrl: 'https://s.ruidcn.cn', // 图片url
    code:'',
    codeImg:'',
    uuid:''
  },
  onLoad: function (options) {
    this.getCode()
  },
  onShow: function () {

  },
  getCode(){
    api.$get(api.main.code).then((res) => {
      this.setData({
        codeImg:res.data.img,
        uuid:res.data.uuid
      })
    })
  },
  //##input 输入##
  input_handle(e) {
    console.log(e.detail.value)
    this.setData({
      [e.currentTarget.dataset.key]: e.detail.value
    })
  },
  //##input 输入##
  //登录
  loginHandle(){ 
    return new Promise((resolve, reject) => {
      var params = {
        username: this.data.username,
        password: this.data.password,
        code: this.data.code,
        uuid: this.data.uuid,
      }
      api.$post(api.main.login, params).then((res) => {
        if(res.code==200){
          wx.setStorageSync('user_id', res.data.userId) 
          wx.setStorageSync('token', res.data.access_token)
          wx.setStorageSync('user_name', this.data.username)
          api.$get(api.main.userInfo).then((userInfo) => {
            wx.setStorageSync('avatar', userInfo.data.avatar) 
            wx.setStorageSync('sex', userInfo.data.sex) 
            wx.setStorageSync('phonenumber', userInfo.data.phonenumber)
            $.toast('登录成功')
          })
          setTimeout(()=>{
            $.linkurl(`/pages/index/index`, 'reLaunch')
          },1500) 
          resolve()
        }
        
      })
    })
  }
})